package org.etom.impl.ui.wizards;

import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Composite;
import org.etom.connector.datatypes.IMoodleModule;

public class DownloadAsWizardPage extends AbstractDownloadAsWizardPage
                                   implements SelectionListener {

  public static final int NEW_PROJECT         = 0;
  public static final int NEW_PROJECT_WIZARD  = 1;
  public static final int IN_EXISTING_PROJECT = 2;
  public static final int NO_NEW_PROJECT      = 4;
  
  private IMoodleModule moodleModule;
  private WizardDialog wizardDialog;
  
  public DownloadAsWizardPage(WizardDialog wizardDialog, IMoodleModule moodleModule) {
    this.wizardDialog = wizardDialog;
    this.moodleModule = moodleModule;
  }
  
  @Override
  public void createControl(Composite parent) {
    super.createControl(parent);
    initEventListeners();
    initControls();
  }

  private void initEventListeners() {
    newProjectWizardButton.addSelectionListener(this);
    newProjectButton.addSelectionListener(this);
    
    newProjectWizardButton.setSelection(true);
    newProjectNameText.setEnabled(false);
  }

  private void initControls() {
    newProjectNameText.setText(moodleModule.getName());
  }
  
  /*===[SelectionListener]===*/
  @Override
  public void widgetDefaultSelected(SelectionEvent e) {
    //nothing to do here
  }

  @Override
  public void widgetSelected(SelectionEvent e) {
    if (e.getSource() == newProjectWizardButton) {
      newProjectNameText.setEnabled(false);
    } else if (e.getSource() == newProjectButton) {
      newProjectNameText.setEnabled(true);
    } else if (e.getSource() == inExistingProjectButton) {
      newProjectNameText.setEnabled(false);
    }
    wizardDialog.updateButtons();
  }
  /*===[SelectionListener]===*/
  
  public int getNewProjectMethod() {
    if (newProjectButton.getSelection())
      return NEW_PROJECT;
    if (newProjectWizardButton.getSelection())
      return NEW_PROJECT_WIZARD;
    if (inExistingProjectButton.getSelection())
      return IN_EXISTING_PROJECT;
    return NO_NEW_PROJECT;
  }
  
  public String getProjectName() {
    return newProjectNameText.getText();
  }
  
}
